package com.i2works.smartluolong.manage.controller.monitor;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.manage.controller.common.CommonItemController;
import com.i2works.smartluolong.manage.service.admin.IAdminRoleService;
import com.i2works.smartluolong.utils.base.FlowItemTypeEnum;
import com.i2works.smartluolong.utils.base.ReportOrderEnum;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.utils.web.HtmlConstant;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.flow.FlowInfoQuery;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 监控统计 - 事项报表控制层
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/6/5 13:52
 */

@Controller
@RequestMapping("/monitorItemReport")
public class MonitorItemReportController extends CommonItemController {


    @Autowired
    private IFlowInfoService flowInfoService;

    public MonitorItemReportController(IAdminRoleService adminRoleService, IFlowTemplateService flowTemplateService, IFlowService flowService, IFlowProcessService flowProcessService) {
        super(adminRoleService, flowTemplateService, flowService, flowProcessService);
    }

    /**
     * 主页
     */
    @GetMapping(value = {"/view/{orderId:\\d+|index}"})
    public String index(Model model, @PathVariable(required = false) String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return HtmlConstant.REDIRECT + HtmlConstant.ERROR_HTML;
        }
        model.addAttribute("itemType", FlowItemTypeEnum.values());
        model.addAttribute("pageTitle", "事项办理");
        //排序列表
        model.addAttribute("reportOrderList", ReportOrderEnum.values());
        //查询数据
        FlowInfoQuery flowInfoQuery = new FlowInfoQuery();
        if ("index".equalsIgnoreCase(orderId) || !NumberUtils.isDigits(orderId)) {
            flowInfoQuery.setOrder(ReportOrderEnum.YEAR.getKey());
        } else {
            flowInfoQuery.setOrder(Integer.parseInt(orderId));
        }
        try {
            List<FlowInfoDO> itemReportList = flowInfoService.queryItemReport(flowInfoQuery);
            //标题列表
            List<String> titleList = Lists.newArrayList();
            //全部统计
            List<Integer> allCountList = Lists.newArrayList();
            Integer allCount = 0;

            //成功的统计
            List<Integer> successCountList = Lists.newArrayList();
            Integer successCount = 0;
            //进行中的统计
            List<Integer> processCountList = Lists.newArrayList();
            Integer processCount = 0;

            if (CollectionUtil.isNotEmpty(itemReportList)) {
                for (FlowInfoDO flowInfoDO : itemReportList) {
                    titleList.add("'" + flowInfoDO.getItemTypeText() + "'");
                    allCountList.add(flowInfoDO.getAllCount());
                    allCount += flowInfoDO.getAllCount();
                    successCountList.add(flowInfoDO.getSuccessCount());
                    successCount += flowInfoDO.getSuccessCount();
                    processCountList.add(flowInfoDO.getProcessCount());
                    processCount += flowInfoDO.getProcessCount();
                }
            }
            model.addAttribute("orderId", orderId);
            model.addAttribute("titleList", titleList);
            model.addAttribute("allCountList", allCountList);
            model.addAttribute("allCount", allCount);
            model.addAttribute("successCountList", successCountList);
            model.addAttribute("successCount", successCount);
            model.addAttribute("processCountList", processCountList);
            model.addAttribute("processCount", processCount);
        } catch (ServiceException e) {
            logger.error("事项报表异常：" + e.getMessage());
        }
        return "/monitor/report_index";
    }

    /**
     * 获取事项名称
     */
    private String getReportItemNames() {
        FlowItemTypeEnum[] values = FlowItemTypeEnum.values();
        List<String> list = Lists.newArrayList();
        for (FlowItemTypeEnum value : values) {
            list.add("'" + value.getValue() + "'");
        }
        String s = BaseUtils.listToStr(list);
        return CollectionUtil.join(list, ",");
    }

}
